import copy

from rest_framework import serializers
from wxuser.serializers import Users
from warehouse.models import JuqiGood, GoodLevel
from warehouse.serializers import JuqiGoodSerializer
from wxcloudrun.ser import BaseSerializer

from .models import (
    YifanshangGame,
    YifanshangSonGame,
    YifanshangGameGood,
    YifanshangRaffleRecord,
)


class YifanshangGameSerializer(BaseSerializer):
    class Meta:
        model = YifanshangGame
        fields = "__all__"


class YifanshangSonGameSerializer(BaseSerializer):
    class Meta:
        model = YifanshangSonGame
        fields = "__all__"


class YifanshangGameGoodSerializer(BaseSerializer):
    detail = serializers.SerializerMethodField()

    class Meta:
        model = YifanshangGameGood
        fields = "__all__"

    def get_detail(self, obj):
        return JuqiGoodSerializer(
            JuqiGood.objects.filter(uuid=obj.good_uuid).first(), many=False
        ).data


class YifanshangRaffleRecordSerializer(BaseSerializer):
    class Meta:
        model = YifanshangRaffleRecord
        fields = "__all__"


def fill_yfs(game):
    game_uuid = game["uuid"]
    game_goods = YifanshangGameGood.objects.filter(game_uuid=game_uuid).all()
    detail_game_info = copy.deepcopy(game)
    goods = []

    for game_good in game_goods:
        good_uuid = game_good.good_uuid
        good = JuqiGood.objects.get(uuid=good_uuid)
        good_s = JuqiGoodSerializer(good)
        good_info = copy.deepcopy(good_s.data)
        good_info["weight"] = game_good.good_weight
        good_info["rarity"] = game_good.good_rarity
        goods.append(good_info)

    detail_game_info["goods"] = goods

    return detail_game_info


def fill_xcx_game_list(games):
    _data = []
    for g in games:
        _data.append(
            {
                "id": g.id,
                "uuid": g.uuid,
                "name": g.name,
                "cover_url": g.cover_url,
                "price": g.price,
            }
        )
    return _data


def fill_game_good(game_good: dict):
    good = JuqiGood.objects.filter(uuid=game_good["good_uuid"]).first()
    level = GoodLevel.objects.filter(uuid=game_good["good_level_uuid"]).first()

    _data = {
        "uuid": game_good.get("uuid"),
        "good_uuid": str(good.uuid),
        "name": good.name,
        "display_url": good.display_url,
        "good_rarity": level.title,
        "count": game_good.get("count"),
        "price": good.price,
        "recovery_price": good.recovery_price,
        "good_type": game_good.get("good_type"),
        "good_presale_type": game_good.get("good_presale_type"),
        "goods_presale_time": game_good.get("goods_presale_time", ""),
    }

    return _data


def fill_yfs_raffle_record(all_raffle_record):
    _data = []

    for index, yrr in enumerate(all_raffle_record):
        jq_good = JuqiGood.objects.filter(uuid=yrr.get("good_uuid")).first()
        good_s = JuqiGoodSerializer(jq_good).data

        son_good = YifanshangGameGood.objects.filter(
            game_uuid=yrr.get("game_uuid"), good_uuid=yrr.get("good_uuid")
        ).first()

        good_level = GoodLevel.objects.filter(uuid=son_good.good_level_uuid).first()

        # 隐私安全问题，返回尽可能少的个人数据
        user = (
            Users.objects.filter(uuid=yrr.get("user_uuid"))
            .filter(deleted=False)
            .first()
        )

        res = {
            "good": {
                "uuid": good_s.get("uuid"),
                "good_name": good_s.get("name"),
                "display_url": good_s.get("display_url"),
                "price": good_s.get("price"),
            },
            "level": good_level.title,
            "user": {"nickname": user.nickname, "head_url": user.head_url},
            "count": index + 1,
            "createdAt": yrr.get("createdAt"),
        }

        _data.append(res)
    return _data
